home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 9 / FM Towns Free Software Collection 9.iso / t_os / tool / tetujin / src / g_eff / ymedflt.c < prev    next >
Text File  |  1994-11-16  |  4KB  |  192 lines

  1. /*
  2.         graphic effect lib.
  3.           Noise Filter
  4.  
  5.         h. Toda 1994 5 9
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <egb.h>
  11. #include "g_eff.h"
  12.  
  13. #define NOERR 0        /* no error */
  14.  
  15. static int mx ;
  16. static int aSen ;
  17. static int mSen ;
  18. static int cMax ;
  19. static int aMax ;
  20. static int x1 ;
  21. static int y1 ;
  22. static int x2 ;
  23. static int y2 ;
  24. static int (*read1)() ;
  25. static int (*write)() ;
  26. static int (*mask)() ;
  27.  
  28. g_yMedianFilter( BASICPARA *para )
  29. {
  30.     unsigned char a[10][4] ;
  31.     int b[10][4] ;
  32.     int r0, g0, b0 ;
  33.     int i, x, y ;
  34.     int n ;
  35.  
  36.     mx = para->mix ;
  37.     aSen = para->alphaSen ;
  38.     mSen = para->maskSen ;
  39.     cMax = para->colorMax ;
  40.     aMax = para->alphaMax ;
  41.     x1 = para->lupx ;
  42.     y1 = para->lupy ;
  43.     x2 = para->rdwx ;
  44.     y2 = para->rdwy ;
  45.     read1 = para->read1 ;
  46.     write = para->write ;
  47.     mask = para->mask ;
  48.  
  49.     for( y = y1 ; y <= y2 ; y++ )
  50.     {
  51.         for( x=x1 ; x <= x2 ; x++ )
  52.         {
  53.             read1( x-1, y-1, a[0] ) ;
  54.             read1( x,   y-1, a[1] ) ;
  55.             read1( x+1, y-1, a[2] ) ;
  56.             read1( x-1, y,   a[3] ) ;
  57.             read1( x,   y,   a[4] ) ;
  58.             read1( x+1, y,   a[5] ) ;
  59.             read1( x-1, y+1, a[6] ) ;
  60.             read1( x,   y+1, a[7] ) ;
  61.             read1( x+1, y+1, a[8] ) ;
  62.  
  63.             for( i=0 ; i<9 ; i++ )
  64.             {
  65.                 rgbToYuv( a[i][0], a[i][1], a[i][2],
  66.                         &(b[i][0]), &(b[i][1]), &(b[i][2]), cMax ) ;
  67.             }
  68.  
  69.             n = ck9(
  70.                      b[0][0], b[1][0], b[2][0],
  71.                      b[3][0], b[4][0], b[5][0],
  72.                      b[6][0], b[7][0], b[8][0]
  73.                    ) ;
  74.  
  75.             yuvToRgb( b[n][0], b[4][1], b[4][2], &r0, &g0, &b0, cMax ) ;
  76.             a[9][0] = r0 ;
  77.             a[9][1] = g0 ;
  78.             a[9][2] = b0 ;
  79.  
  80.             mixWrite( x, y, a[9], a[4] ) ;
  81.         }
  82.     }
  83.  
  84.     return NOERR ;
  85. }
  86.  
  87. static mixWrite( int x, int y, unsigned char *a, unsigned char *b )
  88. {
  89.     unsigned char c[4] ;
  90.     int mix ;
  91.  
  92.     if( mSen )
  93.     {
  94.         if( mask( x, y ) >= mSen )
  95.             return NOERR ;
  96.     }
  97.  
  98.     mix = mx ;
  99.     if( aSen )
  100.     {
  101.         mix = mix * b[3] / aMax ;
  102.     }
  103.  
  104.     c[0] = ( a[0] * mix + b[0] * ( 256 - mix ) + 0x80 ) >> 8 ;
  105.     c[1] = ( a[1] * mix + b[1] * ( 256 - mix ) + 0x80 ) >> 8 ;
  106.     c[2] = ( a[2] * mix + b[2] * ( 256 - mix ) + 0x80 ) >> 8 ;
  107.     c[3] = b[3] ;
  108.  
  109.     if( mix )
  110.     {
  111.         write( x, y, c ) ;
  112.     }
  113.  
  114.     return NOERR ;
  115. }
  116.  
  117. static ck9( int a0, int a1, int a2, int a3,
  118.             int a4, int a5, int a6, int a7, int a8 )
  119. {
  120.     int temp ;
  121.     int m[9] ;
  122.     int a[9] ;
  123.     int i, j ;
  124.  
  125.     for( i=0 ; i<9 ; i++ )m[i] = i ;
  126.  
  127.     a[0] = a0 ; a[1] = a1 ; a[2] = a2 ; a[3] = a3 ; a[4] = a4 ;
  128.     a[5] = a5 ; a[6] = a6 ; a[7] = a7 ; a[8] = a8 ;
  129.  
  130.     for( j=1 ; j<6 ; j++ )
  131.     {
  132.         for( i=j ; i<9 ; i++ )
  133.         {
  134.             if( a[j-1] > a[i] )
  135.             {
  136.                 temp = a[i] ;
  137.                 a[i] = a[j-1] ;
  138.                 a[j-1] =temp ;
  139.  
  140.                 temp = m[i] ;
  141.                 m[i] = m[j-1] ;
  142.                 m[j-1] =temp ;
  143.             }
  144.         }
  145.     }
  146.  
  147.     return m[4] ;
  148. }
  149.  
  150. static rgbToYuv( int r0, int g0, int b0, int *y0, int *u0, int *v0, int max )
  151. {
  152.     int y, cb, cr ;
  153.  
  154.     y  =  19595*r0 + 38470*g0 + 7471*b0 ;
  155.     cb = -11049*r0 - 21699*g0 + 32748*b0 ;
  156.     cr =  32755*r0 -27427*g0 -5328*b0 ;
  157.  
  158.     *y0 = ( ((y/max)*219) >> 16 ) + 16 ;
  159.     *u0 = ( ((cb/max)*224) >> 16 ) + 128 ;
  160.     *v0 = ( ((cr/max)*224) >> 16 ) + 128 ;
  161.  
  162.     if( *y0 < 0 )*y0 = 0 ;
  163.     if( *y0 > 255 )*y0 = 255 ;
  164.     if( *u0 < 0 )*u0 = 0 ;
  165.     if( *u0 > 255 )*u0 = 255 ;
  166.     if( *v0 < 0 )*v0 = 0 ;
  167.     if( *v0 > 255 )*v0 = 255 ;
  168.  
  169.     return NOERR ;
  170. }
  171.  
  172. static yuvToRgb( int y0, int u0, int v0, int *r0, int *g0, int *b0, int max )
  173. {
  174.     y0 = (( y0 - 16 ) * max )/219 ;
  175.     u0 = (( u0 -128 ) * max )/224 ;
  176.     v0 = (( v0 -128 ) * max )/224 ;
  177.  
  178.     *r0 = ( 65536*y0 + 4*u0 + 91920*v0 ) >> 16 ;
  179.     *g0 = ( 65536*y0 - 22569*u0 - 46819*v0 ) >> 16 ;
  180.     *b0 = ( 65536*y0 + 116198*u0 - 9*v0 ) >> 16 ;
  181.  
  182.     if( *r0 < 0 )*r0 = 0 ;
  183.     if( *r0 > max )*r0 = max ;
  184.     if( *g0 < 0 )*g0 = 0 ;
  185.     if( *g0 > max )*g0 = max ;
  186.     if( *b0 < 0 )*b0 = 0 ;
  187.     if( *b0 > max )*b0 = max ;
  188.  
  189.     return NOERR ;
  190. }
  191.  
  192.